#!/usr/bin/env python
# -*- coding:utf-8 -*-

import bpy
import bmesh

try:
    bpy.ops.object.mode_set(mode = 'OBJECT')
    bpy.ops.object.select_all(action = 'SELECT')
    bpy.ops.object.delete()
except:
    pass

# 创建立方体并且从顶面挤出
bpy.ops.mesh.primitive_cube_add(radius = 0.5, location = (-3, 0, 0))
bpy.ops.object.mode_set(mode = 'EDIT')
bpy.ops.mesh.select_all(action = 'DESELECT')

# 设置为face模式
bpy.ops.mesh.select_mode(type = 'FACE')

bm = bmesh.from_edit_mesh(bpy.context.object.data)
bm.faces.ensure_lookup_table()
bm.faces[5].select = True
bpy.ops.mesh.extrude_region_move(TRANSFORM_OT_translate
                                 = {
                                     "value": (0.3, 0.3, 0.3),
                                     "constraint_axis": (True, True, True),
                                     "constraint_orientation": "NORMAL",
                                 })
bpy.ops.object.mode_set(mode='OBJECT')


# Create a cube and subdivide the top face
bpy.ops.mesh.primitive_cube_add(radius=0.5, location=(0, 0, 0))
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action="DESELECT")
bm = bmesh.from_edit_mesh(bpy.context.object.data)
bm.faces.ensure_lookup_table()
bm.faces[5].select = True
bpy.ops.mesh.subdivide(number_cuts = 1)
bpy.ops.mesh.select_all(action="DESELECT")
bm.faces.ensure_lookup_table()
bm.faces[5].select = True
bm.faces[7].select = True
bpy.ops.transform.translate(value = (0, 0, 0.5))
bpy.ops.object.mode_set(mode='OBJECT')


# Create a cube and add a random offset to each vertex
bpy.ops.mesh.primitive_cube_add(radius=0.5, location=(3, 0, 0))
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action="SELECT")
bpy.ops.transform.vertex_random(offset = 0.5)
bpy.ops.object.mode_set(mode='OBJECT')